﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class HeadSkill : Item
{
    public float frozenTimeLimit = 2;
    float _power;
    public override float power
    {
        get { return _power; }
        set { _power = value; }
    }
    public enum SkillState
    {
        Idle = 0,
        Running = 1,
        Frozen = 2
    }
    public SkillState skillState;
    public override bool CanWork => true; // the head is always working
    public override bool Following => false; // the head will never follow things
    public override bool Recovering {
        get
        {
            return skillState == SkillState.Idle;
        }
    }

    // Start is called before the first frame update
    void Start()
    {
        type = ItemType.Head;
        workingTarget = GetComponent<Head>();
        follow = false; // the head will never follow things
        working = true; // the head is always working
        power = powerMax;
    }

    // Update is called once per frame
    void Update()
    {
        if (skillState == SkillState.Frozen)
        {
            frozenTimer += Time.deltaTime;
            if (frozenTimer > frozenTimeLimit)
            {
                frozenTimer = 0;
                skillState = SkillState.Idle;
            }
        }
        UpdatePower();
        // GetComponentInChildren<Text>().text = ((int)power).ToString() + "/" + ((int)powerMax).ToString();
    }

    public float frozenTimer = 0;

    public bool QuerySkill(ForceParam.InputType inputType)
    {
        if (inputType == ForceParam.InputType.None)
        {
            if (skillState == SkillState.Running)
                skillState = SkillState.Frozen;
            return true;
        }
            
        if (skillState == SkillState.Frozen)
        {
            
            return false;
        } else
        {
            skillState = SkillState.Running;
            return true;
        }
    }

    public override void StartWorking()
    {

    }

    public override void EndWorking()
    {
        
    }

    public override void UpdatePower()
    {
        if (skillState == SkillState.Idle)
        {
            power = Mathf.Min(power + Time.deltaTime * powerRecoveryRate, powerMax);
        } else if (skillState == SkillState.Running)
        {
            power = power - Time.deltaTime * powerConsumeRate;
            if (power < 0)
            {
                power = 0;
                skillState = SkillState.Frozen;
            }
        }
    }
}
